// 刷新图片  
function changeImg() {
    var imgSrc = $("#imgObj");  
    var src = imgSrc.attr("src");  
    imgSrc.attr("src", changeUrl(src));
}  
//为了使每次生成图片不一致，即不让浏览器读缓存，所以需要加上时间戳  
function changeUrl(url) {  
    var timestamp = (new Date()).valueOf();  
    var index = url.indexOf("?",url);  
    if (index > 0) {
        url = url.substring(0, index); 
    }  
    if ((url.indexOf("&") >= 0)) {  
        url = url + "×tamp=" + timestamp;  
    } else {  
        url = url + "?timestamp=" + timestamp;  
    }  
    return url;  
} 

//获取短信验证码
var msgFlag = true;
var timeFlag = true;
var pattern = /^1[34578]\d{9}$/;
function getMessage() {
	$(this).css("background", "#dedede");
	if (msgFlag && timeFlag) {
		msgFlag = false;
		timeFlag = false;
		$.ajax({
			url : path + "/interface/sms.html",
			data : {
				mobile : $(".user-number").val(),
			},
			type : "GET",
			datatype : "json",
			async : false,
			success : function(datas) {
				var json = eval("(" + datas + ")");
				msgFlag = true;
				if (json.msg == "1") {
					yanzhengma = json.data;
				} else if (json.msg == "2") {
					changeImg();
					layer.msg(json.data);
				}
			}
		});
		n = 60;
		var time = setInterval(getTime, 1000)
		function getTime() {
			n--;
			$(".sms-code").text(n + "s");
			if (n < 0) {
				timeFlag = true;
				changeImg();
				$(".sms").removeClass("sms-wrap");
				clearInterval(time);
				$(".sms-code").text("获取");
			}
		}
	}
}

// 验证图片验证码
function checkyanzhengma() {
	var code = $(".user-yan").val();
	if (getByteLen(code) == 4) {
		$.ajax({
			url : path + "/user/checkCode.html",
			data : {
				code : code,
			},
			type : "GET",
			datatype : "json",
			async : false,
			success : function(datas) {
				var json = eval("(" + datas + ")")
				if (json.msg == 1) {
					$(".sms").removeClass("sms-wrap");
					$(".sms-code").css("background", "#dedede");
				} else {
					$(".sms").addClass("sms-wrap");
					$(".sms-code").css("background", "#fedf44");
				}
			}
		});
	} else {
		$(".sms").removeClass("sms-wrap");
		$(".sms-code").css("background", "#dedede");
	}
}

// 获取字符串长度（汉字算两个字符，字母数字算一个）
function getByteLen(val) {
	var len = 0;
	for (var i = 0; i < val.length; i++) {
		var a = val.charAt(i);
		if (a.match(/[^\x00-\xff]/ig) != null) {
			len += 2;
		} else {
			len += 1;
		}
	}
	return len;
}
